% grid  MT profile data stes
%latest date 1.8.4

function[X1,Y1,R,P,RER,PER] = mtgrid(Prof,xres,yres),
%xres in km and y res in log10(seconds)
method = 'cubic';
np = 1;

for i = 1:length(Prof),
    for j = 1:length(Prof(i).frq),
        
        X(np) = Prof(i).dist;
        Y(np) = log10(1/Prof(i).frq(j)); %log of period
        R(np) = log10(Prof(i).res(j)); % log of res
        P(np)= Prof(i).ph(j);
        RER(np) = log10(Prof(i).reser(j));
        PER(np) = Prof(i).pher(j);
        
        np = np+1;
    end;
end;

np = np-1;
L = P < 0 | P >90;

X(L) = [];
Y(L) = [];
R(L) = [];
P(L) = [];
RER(L) = [];
PER(L) = [];

[X1,Y1] = meshgrid(min(X):xres:max(X),min(Y):yres:max(Y));

[R] = griddata(X,Y,R,X1,Y1,method);
[P] = griddata(X,Y,P,X1,Y1,method);
[RER] = griddata(X,Y,RER,X1,Y1,method);
[PER] = griddata(X,Y,PER,X1,Y1,method);